﻿param (
    [string]$env = "dev"
)

$site_env  = "dev"
$site_prefix  = ""

if($env -eq "dev" )
{
    # with default
}
elseif ($env -eq "pre" )
{
    $site_env = "preview"
    $site_prefix = ""
}
elseif ($env -eq "uat" )
{
    $site_env = "uat"
    $site_prefix = ""
}
else
{
    echo "Invalid environment argument"
    Exit
}

$appPoolNames = 'dev.dcc', 'dev.trunklpg', 'dev.saasapplpg', 'dev.wxshopapi', 'dev.webapi','dev.webapi_lpg'
$websiteNames = 'Master', 'Slave'
$doubleQoute = [char]34

iex 'NET STOP W3SVC'
iex 'NET STOP MSSQLSERVER'

echo 'Preparing configuration'

$hostIPAddress = (Get-Wmiobject -class win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').ipaddress[0]

#$path = $PSScriptRoot
#$path = $path + "\..\..\.."
#$path = Resolve-Path $path
$path = "C:\WEBSITE"

foreach ($siteName in $websiteNames)
{
    echo "`nDropping Website"
    $command = "cmd /c '%windir%\system32\inetsrv\appcmd delete site /site.name:$doubleQoute$siteName$doubleQoute'"
    iex $command

    echo "`nDropping Application Pool"
    $poolName = $siteName
    $command = "cmd /c '%windir%\system32\inetsrv\appcmd delete apppool /apppool.name:$doubleQoute$poolName$doubleQoute'"
    iex $command
    
    foreach ($appPoolName in $appPoolNames)
    {
        echo "`nDropping Application Pool"
        $poolName = "${siteName}.${appPoolName}"
        $command = "cmd /c '%windir%\system32\inetsrv\appcmd delete apppool /apppool.name:$doubleQoute$poolName$doubleQoute'"
        iex $command
    }
}

$index = 9001
echo "`nImporting Website"
foreach ($siteName in $websiteNames)
{
    $mode = "Classic"
    $version = "v4.0"
    
    $poolName = $siteName
    $template = Get-Content "app_pool.xml.template" | Out-String
    $template = $template -replace "%poolName%", $poolName
    $template = $template -replace "%mode%", $mode
    $template = $template -replace "%version%", $version
    $template | Out-String | Out-File ".\sites\app_pool.${poolName}.xml" -encoding 'UTF8'
    echo "`nImporting Application Pool"
    iex "cmd /c '%windir%\system32\inetsrv\appcmd add apppool /in < .\sites\app_pool.$doubleQoute$poolName$doubleQoute.xml'"
    
    foreach ($appPoolName in $appPoolNames)
    {
        $poolName = "${siteName}.${appPoolName}"
        # net6使用无托管代码
        if($poolName.contains("webapi"))
        {
            $mode = "Integrated"
            $version = ""
        }
        else
        {
            $mode = "Classic"
            $version = "v4.0"
        }
        $template = Get-Content "app_pool.xml.template" | Out-String
        $template = $template -replace "%poolName%", $poolName
        $template = $template -replace "%mode%", $mode
        $template = $template -replace "%version%", $version
        $template | Out-String | Out-File ".\sites\app_pool.${poolName}.xml" -encoding 'UTF8'
        echo "`nImporting Application Pool"
        iex "cmd /c '%windir%\system32\inetsrv\appcmd add apppool /in < .\sites\app_pool.$doubleQoute$poolName$doubleQoute.xml'"
    }
    
    $template = Get-Content "site.xml.template" | Out-String
    $template = $template -replace "%site_name%", $siteName
    $template = $template -replace "%site_base_num%", $index
    $template = $template -replace "%site_env%", $site_env
    $template = $template -replace "%site_prefix%", $site_prefix
    $template = $template -replace "%website_root%", "${path}\${siteName}"
    $template = $template -replace "%hostIP%", $hostIPAddress
    $template = $template -replace "%site_base_port%", $index
    $template | Out-String | Out-File ".\sites\site.${siteName}.xml" -encoding 'UTF8'
    
    echo "`nImporting Web Site"
    $command = "cmd /c '%windir%\system32\inetsrv\appcmd add site /in < .\sites\site.$doubleQoute$siteName$doubleQoute.xml'"
    iex $command
    
    $index++
}

iex "NET START W3SVC"
iex "NET START MSSQLSERVER"